#Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# class Solution:
#     def reverseList(self, head: ListNode) -> ListNode:
#         if not head:
#             return None
#         if not head.next:
#             return head
#         headNode = self.reverseList(head.next)
#         head.next.next = head
#         head.next = None
#         return headNode
#

class Node(object):
    # 定义方法，初始值为空
    def __init__(self, value=None, next=None):
        self.value = value
        self.next = next

    # 使用staticmethod装饰的方法，称为静态方法
    # 可以接收参数也可以不接受参数 ，参数不代表实例也不代表类
    @staticmethod
    def reverse(head):
        print(head)
        cur_node = head  # 当前节点
        new_link = None  # 表示反转后的链表
        while cur_node != None:
            tmp = cur_node.next  # cur_node后续节点传递给中间变量
            cur_node.next = new_link    # cur_node指向new_link
            new_link = cur_node    # 反转链表更新,cur_node为新的头结点
            cur_node = tmp    # 原链表节点后移一位
        return new_link

link = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9)))))))))
root = Node.reverse(link)
while root:
        print(root.value)
        root =root.next
        print(root,"s")
